package com.hqu.ms.utils;

import java.io.File;
import java.lang.ref.WeakReference;
import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.Executor;

import com.hqu.ms.R;

 
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.widget.ImageView;
 
/**
 * 
 * @author Juraj
 * 
 */
public class ImageGetterAsync {
	private Context mContext;
	private Resources mResources;
	private Drawable mDefaultDrawable;
	private Executor mExecutor;
 
	private static final Map<String, WeakReference<Drawable>> mDrawableCache = Collections.synchronizedMap(new WeakHashMap<String, WeakReference<Drawable>>());
 
	public ImageGetterAsync(Context context, Executor executor) {
		mContext = context;
		mResources = mContext.getResources();
 
		mDefaultDrawable = mResources.getDrawable(R.drawable.ic_launcher);
		mDefaultDrawable.setBounds(0, 0, mDefaultDrawable.getIntrinsicWidth(), mDefaultDrawable.getIntrinsicHeight());
 
		mExecutor = executor;
	}
	
	
	public void getSyncImage(String imageUrl,ImageView imageView){
		if(mDrawableCache.containsKey(imageUrl) && mDrawableCache.get(imageUrl).get() != null){
			imageView.setImageDrawable(mDrawableCache.get(imageUrl).get());
			return ;
		}
		ImageDownloadData imageDownloadData = new ImageDownloadData(imageView, imageUrl);
		ImageDownloadAsyncTask imageDownloadTask = new ImageDownloadAsyncTask(imageDownloadData);
		imageDownloadTask.executeOnExecutor(mExecutor, null);
	}
	
	public static void invalidate() {
		mDrawableCache.clear();
	}
 
	/**
	 * 
	 * @author Juraj
	 * 
	 */
	class ImageDownloadData {
		private ImageView imageView;
		private String source;
 
		public ImageDownloadData(ImageView imageView, String source) {
			this.imageView = imageView;
			this.source = source;
		}
 
		public ImageView getImageView() {
			return this.imageView;
		}
 
		public String getSource() {
			return this.source;
		}
	}
 
	/**
	 * 
	 * @author Juraj
	 * 
	 */
	class ImageDownloadAsyncTask extends AsyncTask<Void, Void, Drawable> {
		private ImageDownloadData imageDownloadData;
 
		public ImageDownloadAsyncTask(ImageDownloadData imageDownloadData) {
			this.imageDownloadData = imageDownloadData;
		}
 
		@Override
		protected Drawable doInBackground(Void... params) {
			try {
				String sourceUrl = imageDownloadData.getSource();
				
				File file = ImageTools.downloadFileCache(sourceUrl, mContext);
				if (file == null) {
					file = ImageTools.downloadFileWeb(sourceUrl, mContext);
				}
 
				if (file == null) {
					return null;
				}
 
				Bitmap bitmap = ImageTools.getBitmapFromFile(file);
				if (bitmap == null) {
					return null;
				}
 
				Drawable drawable = new BitmapDrawable(mResources, bitmap);
				drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
				return drawable;
 
			} catch (Throwable t) {
			}
			return null;
		}
 
		@Override
		protected void onPostExecute(Drawable drawable) {
			if (!mDrawableCache.containsKey(imageDownloadData.getSource())) {
				mDrawableCache.put(imageDownloadData.getSource(), new WeakReference<Drawable>(drawable));
			}
			
			ImageView imageView = imageDownloadData.getImageView();
			imageView.setImageDrawable(drawable);
			
		}
	}
}
